<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-if="isAdmin || isAudit"> 新增</a-button>
        <a-button type="error" @click="batchDelete" v-if="isAdmin || isAudit"> 批量删除</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record),
              ifShow: isUser,
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              tooltip: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
              ifShow: isAudit || isAdmin,
            },
          ]"
        />
      </template>
    </BasicTable>
    <OpmsFixFamilyInfoDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import OpmsFixFamilyInfoDrawer from './OpmsFixFamilyInfoDrawer.vue';
  import { columns, searchFormSchema } from './opmsFixFamilyInfo.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { OpmsFixFamilyInfoIDType } from '/@/api/opms/model/OpmsFixFamilyInfoModel';
  import {
    deleteOpmsFixFamilyInfoBatch,
    getOpmsFixFamilyInfoPage,
  } from '/@/api/opms/opmsFixFamilyInfo';
  import { getUserInfo } from '/@/api/sys/user';
  import { useUserStore } from '/@/store/modules/user';

  // 初始化弹出模块
  const [registerDrawer, { openDrawer }] = useDrawer();
  // 消息提示
  const { createMessage, createConfirm } = useMessage();
  const user: any = getUserInfo();
  const { isAdmin, isAudit, isUser } = useUserStore();
  // 初始化表格
  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '常住家庭信息表列表',
    api: getOpmsFixFamilyInfoPage,
    columns,
    formConfig: {
      labelWidth: 140,
      schemas: searchFormSchema,
    },
    clickToRowSelect: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,

    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    console.log('用戶信息', user);
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    let params: Array<OpmsFixFamilyInfoIDType> = [];
    params.push(record.id);
    await deleteOpmsFixFamilyInfoBatch(params);
    createMessage.success('删除成功');
    await reload();
  }

  function handleSuccess() {
    reload();
  }

  // 批量删除
  async function batchDelete() {
    let params: Array<OpmsFixFamilyInfoIDType> = [];
    for (let _item of getSelectRows()) {
      params.push(_item.id);
    }
    if (params.length <= 0) {
      createMessage.warning('请至少选择一条记录');
      return;
    }

    createConfirm({
      iconType: 'warning',
      title: () => '删除提示',
      content: () => '确定要删除选中项吗！',
      okText: () => '立即删除',
      cancelText: () => '取消',
      onOk: async () => {
        await deleteOpmsFixFamilyInfoBatch(params);
        createMessage.success('删除成功');
        // 清空选择行
        clearSelectedRowKeys();
        await reload();
      },
    });
  }
</script>
<script lang="ts">
  export default { name: 'OpmsFixFamilyInfo' };
</script>
